package com.xckj.cycling.framework

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import com.junca.android.base.MvvmUtils
import com.xckj.cycling.R
import com.xckj.cycling.view.dialog.LoadingDialog

/**
 * @Author: liyang
 * @Describe: kt Activity的基类
 * @Date: 2023/2/25 22:02
 */
abstract class BaseActivityKt : AppCompatActivity(), IBaseView {

    private var mImmersionBar: ImmersionBar? = null

    private var loadingDialog: LoadingDialog? = null
    private var dialogCount = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MvvmUtils.findViewBinding(this, window?.decorView as? ViewGroup, LayoutInflater.from(this))
            ?.apply {
                setContentView(this.root)

//                val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, QMUIStatusBarHelper.getStatusbarHeight(baseContext))
//                findViewById<View>(R.id.common_top_bar_view)?.layoutParams = FrameLayout.LayoutParams(params)

                initView(this.root, savedInstanceState)
            }
    }

    protected open fun initView(root: View, savedInstanceState: Bundle?) {
    }

    protected fun notifyViewBinding() {
        setTitle()
        initData()
        setListener()
        initStatusBar()
    }


    protected open fun setTitle() {}

    /**
     * 初始化数据
     */
    protected open fun initData() {

    }

    /**
     * 设置监听
     */
    protected open fun setListener() {

    }

    /**
     * 初始化沉浸式状态栏
     */
    private fun initStatusBar() {
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init()
        }
    }

    /**
     * 是否使用沉浸式状态栏
     *
     * @return true or false
     */
    protected open fun isStatusBarEnabled(): Boolean {
        return true
    }

    /**
     * 状态栏字体是否深色模式
     *
     * @return true or false
     */
    protected open fun isStatusBarDarkFont(): Boolean {
        return true
    }

    /**
     * 初始化沉浸式状态栏
     *
     * @return ImmersionBar
     */
    protected open fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this) /*默认状态栏字体颜色为黑色*/
            .statusBarDarkFont(isStatusBarDarkFont()).keyboardEnable(false) /*指定导航栏背景颜色*/
            .navigationBarColor(R.color.white) /*状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色*/
            .autoDarkModeEnable(true).autoNavigationBarDarkModeEnable(true)
    }

    /**
     * 获取状态栏沉浸的配置对象
     *
     * @return ImmersionBar
     */
    open fun getStatusBarConfig(): ImmersionBar {
        return mImmersionBar ?: createStatusBarConfig().also { mImmersionBar = it }
    }

    override fun showMessage(message: CharSequence?) {
        if (!TextUtils.isEmpty(message)) {
            runOnUiThread {
                Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun showLoading() {
        runOnUiThread {
            dialogCount++
            if (null == loadingDialog || !loadingDialog!!.isShowing()) {
                loadingDialog = LoadingDialog(this).apply {
                    create()
                }
            }
            if (loadingDialog?.isShowing() != true) {
                loadingDialog?.show()
            }
        }
    }

    override fun hideDialog() {
        runOnUiThread {
            dialogCount--
            Log.e("hideDialog", "loading Count$dialogCount")
            if (null == loadingDialog) {
                return@runOnUiThread
            }
            if (dialogCount <= 0) {
                loadingDialog!!.dismiss()
            }
        }
    }
}